<template>
 <div class="result">
   <van-nav-bar
      :title="keyword + '的搜索结果'"
      left-arrow
      @click-left="$router.back()"
      fixed
    >
    </van-nav-bar>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in list"
        :key="item.art_id.toString()"
        :title="item.title"
        @click="$router.push(`/article/${item.art_id.toString()}`)"
      />
    </van-list>
 </div>
</template>

<script>
import { getSearch } from '@/api/search'
export default {
  name: 'serachResult',
  computed: {
    keyword () {
      return this.$route.query.keyword
    }
  },
  data () {
    return {
      loading: false,
      finished: false,
      list: [],
      page: 1
    }
  },
  methods: {
    async onLoad () {
      const res = await getSearch(this.keyword, this.page)

      this.list = [...this.list, ...res.data.results]
      this.loading = false
      this.page++
      if (res.data.results.length === 0 || this.list.length >= 50) {
        this.finished = true
      }
    }
  }
}
</script>

<style>
.result {
  padding-top: 46px;
}
</style>
